<template>
  <el-dialog
    :title="edittype === 'edit' ? '编辑部门' : '新增部门'"
    :visible.sync="dialogVisible"
    width="700px"
    :append-to-body="true"
    :close-on-click-modal="false"
    @close="dialogCancel"
  >
    <el-form ref="formData" v-loading="loading" element-loading-text="加载中..." label-width="auto" status-icon :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" placeholder="部门名称" size="mini" style="width:450px;" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" placeholder="部门编码" size="mini" style="width:450px;" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" placeholder="请选择活动区域" size="mini" style="width:450px;">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" size="mini" :rows="4" placeholder="1-100个字符" />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" @click="confirm">确定</el-button>
        <el-button size="mini" @click="dialogCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getManagerList, addDepartment, getDepartmentDetail, updateDepartment } from '@/api/department.js'
export default {
  name: 'Showdialog',
  props: {
    id: {
      type: Number,
      Required: true,
      default: null
    },
    // 编写状态 新增 / 编辑
    edittype: {
      type: String,
      Required: true,
      default: null
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      managerList: [],
      defaultData: {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人id
        name: '', // 部门名称
        pid: this.id.toString() // 父级部门的id
      },
      formData: { ...this.defaultData },
      rules: {
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur' },
          // 部门编码不能和原先存在的编码相同
          { validator: (rule, value, callback) => {
            let result = this.DefaultData()
            if (this.edittype === 'edit') {
              // 先把本身除去 因为可以和自身相同
              result = result.filter(item => { return item.id !== this.id })
            }
            if (result.some((item) => item.code === value)) {
              callback(new Error('部门已经存在此编码的了'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ], // 部门编码
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍的长度为1-50个字符', trigger: 'blur' }
        ], // 部门介绍
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ], // 部门负责人id
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur' },
          // 部门名称不能和原先存在的名称相同
          { validator: (rule, value, callback) => {
            let result = this.DefaultData()
            if (this.edittype === 'edit') {
              // 先把本身除去 因为可以和自身相同
              result = result.filter(item => { return item.id !== this.id })
            }
            if (result.some((item) => item.name === value)) {
              callback(new Error('部门名字不允许重复'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ] // 部门名称
        // pid: '' // 父级部门的id 不需要做校验
      }
    }
  },
  created() {
    this.GetmanagerList()
    this.SetDefaultData()
  },
  methods: {
    // 初始化部门 获取部门负责人
    async GetmanagerList() {
      const res = await getManagerList()
      this.managerList = res
    },
    // 将formData设置默认defaultData
    SetDefaultData() {
      this.formData = { ...this.defaultData }
    },
    // 确定 添加/编辑 子部门
    async confirm() {
      this.$refs.formData.validate() // 表单再次校验
      let msg = '添加子部门成功'
      if (this.edittype === 'edit') {
        await updateDepartment(this.id, this.formData)
        msg = '编辑部门成功'
      } else {
        await addDepartment(this.formData)
      }
      this.dialogCancel()
      this.$message.success({ message: msg, duration: 1500 })
      this.GetDepartment()
    },
    // 关闭弹框
    dialogCancel() {
      this.formData = { ...this.defaultData } // 表单为空
      this.dialogVisible = false // 关闭弹框
      this.$refs.formData.resetFields() // 表单校验重置
    },
    // 获取编辑部门详情
    async GetDepartmentDetail() {
      this.loading = true
      const res = await getDepartmentDetail(this.id.toString()) // 回显
      this.formData = res
      this.loading = false
    }
  },
  inject: ['GetDepartment', 'DefaultData'] // 子孙通信
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog .el-dialog__body{
      display: flex;
      justify-content: center;
      align-items: center;
}
</style>
